//
//  AppDelegate.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//

//需要看看是否为新版本
//登录状态 把BaseTabelViewController的修改一下就好
//监听通知
//接受来自三个地方的通知

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //打印保存到沙盒的东西
        let account = userAccount.loadAccount()
        print(account)
        
    
        window  = UIWindow(frame: UIScreen.mainScreen().bounds)
//==============================\\
        //主题色
         setThemeColor()
 //==============================\\
        //注册通知
        registerNotification()
//==============================\\
        window?.backgroundColor = UIColor.greenColor()
        
         window?.makeKeyAndVisible()
//    未登录界面
//        window?.rootViewController = MainTableViewController()
        //欢迎界面
//        window?.rootViewController = WelcomeViewController()
        //新特性界面
//        window?.rootViewController = NewFeatureCollectionViewController()
        
//==============================================================\\
        //不确定,看状态跳转控制器
        window?.rootViewController = defaultRootController()
//==============================================================\\
        return true
         }
    //主题色
    private func setThemeColor(){
    
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    
    
    }
    //监听通知
    
    private func registerNotification(){
        
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "I_am_Pig:", name: AppSwitchRootViewControler, object: nil)
    
    }
    //通知的用处
    @objc private func I_am_Pig(nn : NSNotification){
    
        print(nn)
        
        if(nn.object as?String) != nil{
        
            window?.rootViewController = WelcomeViewController()
            
            return
        }
        
    window?.rootViewController = MainTableViewController()
        
    }
    //移除通知
    deinit{
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    
    //判断用户是否登录,是否更新版本
    
    private func defaultRootController() ->UIViewController {
    
        //是否登录
        if userAccountViewModel().userLogin {
        
//            if isNewVersion() {
//            
//            return NewFeatureCollectionViewController()
//                
//            }
//            return WelcomeViewController()
            
//            三目运算,一步搞定
            return isNewVersion() ? NewFeatureCollectionViewController() : WelcomeViewController()
            
        }
        return MainTableViewController()
    }
    
  private  func isNewVersion() ->Bool{
    
    //比较新旧版本
    //1.获取新版本
    let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
    let versionNum = Double(versionStr)
    //2.获取 旧版本  一般存放在沙盒3
    
    let key = "sandBoxKey"
    let userdefaults = NSUserDefaults.standardUserDefaults()
    let lastVersion = userdefaults.doubleForKey(key)
    
    //3.保存新版本
    userdefaults.setDouble(versionNum!, forKey: key)
    //同步沙盒
    userdefaults.synchronize()
    
    //4.比较新旧版本
    return versionNum! > lastVersion

    
    }
    

}

 